import { createRouter, createWebHistory } from 'vue-router/auto';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import settingTitle from '@/router/method/settingTitle';
import { isLogin } from '@/utils/auth';

//导入生成的路由数据
const router = createRouter({
	history: createWebHistory(),
});

router.beforeEach(async (_to, _from, next) => {
	NProgress.start();
	// 设置标题
	settingTitle(_to.meta.title);

	if (!isLogin() && _to.path !== '/login') {
		next({
			path: '/login',
		});
	} else {
		_to.path === '/' ? next({ path: '/home' }) : next();
	}
});

router.afterEach(_to => {
	NProgress.done();
});

export default router;
